package br.com.cogerh.sidec.dao;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import br.com.cogerh.sidec.model.Compromisso;
import br.com.cogerh.sidec.model.Reuniao;
import br.com.cogerh.sidec.util.HibernateUtil;

public class ReuniaoImpl implements ReuniaoDAO {

	@Override
	public void salvar(Reuniao reuniao) {
		Session sessao = HibernateUtil.getSessionFactory().getCurrentSession();
		Transaction tx = sessao.beginTransaction();
		try {
			sessao.save(reuniao);
			sessao.flush();
			sessao.close();
			tx.commit();
			System.out.println("Reuniao Salvo com Sucesso!");
		} catch (HibernateException e) {
			tx.rollback();
		}

	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Reuniao> listar() {
		Session sessao = HibernateUtil.getSessionFactory().getCurrentSession();
		Transaction tx = sessao.beginTransaction();
		List<Reuniao> lista = new ArrayList<Reuniao>();

		try {
			Criteria criteria = sessao.createCriteria(Reuniao.class);
			criteria.addOrder(Order.desc("dataReuniao"));
			lista = criteria.list();
			sessao.close();
		} catch (HibernateException e) {
			tx.rollback();
		}

		return lista;
	}

	@Override
	public void excluir(Reuniao reuniao) {
		Session sessao = HibernateUtil.getSessionFactory().getCurrentSession();
		Transaction tx = sessao.beginTransaction();
		try {
			sessao.delete(reuniao);
			sessao.flush();
			sessao.close();
			tx.commit();
			System.out.println("Reuniao Excluido com Sucesso!");

		} catch (HibernateException e) {
			tx.rollback();
		}

	}

	@Override
	public void editar(Reuniao reuniao) {
		Session sessao = HibernateUtil.getSessionFactory().getCurrentSession();
		Transaction tx = sessao.beginTransaction();
		try {
			sessao.merge(reuniao);
			sessao.flush();
			sessao.close();
			tx.commit();
			System.out.println("Reuniao Editado com Sucesso!");
		} catch (HibernateException e) {
			tx.rollback();
		}

	}

	@Override
	public void salvarListaReuniao(List<Reuniao> reunioes) {
		Session sessao = HibernateUtil.getSessionFactory().getCurrentSession();
		Transaction tx = sessao.beginTransaction();
		try {
			for (Reuniao reuniao2 : reunioes) {
				sessao.save(reuniao2);
				System.out.println("Reuniao Salva!");

			}

			sessao.flush();
			sessao.close();
			tx.commit();
		} catch (HibernateException e) {
			tx.rollback();
		}

	}

	@Override
	public List<Reuniao> listarByNome(String nome) {
		Session sessao = HibernateUtil.getSessionFactory().getCurrentSession();
		Transaction tx = sessao.beginTransaction();
		List<Reuniao> lista = new ArrayList<Reuniao>();
	try{
		Criteria criteria = sessao.createCriteria(Reuniao.class);

		criteria.addOrder(Order.desc("dataReuniao"));
		criteria.add(Restrictions.eq("nome", nome));
		lista = criteria.list();
		sessao.close();
	}catch(HibernateException e){
		tx.rollback();
	}
	
		
		return lista;
	}

	@Override
	public void fecharSessao() {

	}

	@Override
	public List<Reuniao> listarByPessoa(Integer id) {
		Session sessao = HibernateUtil.getSessionFactory().getCurrentSession();
		Transaction tx = sessao.beginTransaction();
		List<Reuniao> lista = new ArrayList<Reuniao>();
		try{
			Criteria criteria = sessao.createCriteria(Reuniao.class);
			criteria.addOrder(Order.desc("dataReuniao"));
			criteria.add(Restrictions.eq("nome", id));
			lista = criteria.list();
			sessao.close();
		}catch(HibernateException e){
			tx.rollback();
		}
	
		return lista;
	}

}
